local Lang = require("common.Lang")
local Log = require("common.Log")
local Config = require("Config")
local Msg = require("core.Msg")
local Obj = require("core.Obj")
local Timer = require("core.Timer")
local Gm = require("chat.Gm")
local ChatMsgSolve = require("chat.ChatMsgSolve")
local GmCommon = require("chat.GmCommon")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local BagLogic = require("bag.BagLogic")
local ShopHelp = require("shop.ShopHelp")

local ItemDefine = require("bag.ItemDefine")

local ObjHuman = require("core.ObjHuman")

local Dict2 = require("core.Dict2")
local Dict3 = require("core.Dict3")

CHAT_TYPE_WORLD = 1		--世界
CHAT_TYPE_GUILD = 2		--军团
CHAT_TYPE_TEAM = 3		--队伍
CHAT_TYPE_PRIVATE = 4	--私聊
CHAT_TYPE_XITONG = 5	--系统
CHAT_TYPE_LABA = 6		--喇叭
CHAT_TYPE_NEAR = 7		--附近
CHAT_TYPE_ACTIVITY = 8	--活动
CHAT_TYPE_GROSSIP = 9	--传闻
CHAT_TYPE_ADVERTISE = 10--广告


local ban_time_max = 9999999999999

function CG_CHAT(human, recv_msg)
	Log.write(Log.LOGID_OSS_CHAT, human._id, human.account, human.name, human.ip, recv_msg.msg_type, GmCommon.filter(recv_msg.msg, Dict3))
	if Gm.checkGm(human, recv_msg.msg) then
		return		
	end
	
	if not Config.IS_USE_GM_CMD and human.db.gm == 0 then
		if recv_msg.msg_type == CHAT_TYPE_WORLD then --世界聊天
			if human.db.lv < 35 then
				return
			end
			if human.worldChatTime and human.worldChatTime + 4000 > Timer.now then
				return
			end
			human.worldChatTime = Timer.now
		end
		if recv_msg.msg_type == CHAT_TYPE_LABA then -- 喇叭
			if human.db.lv < 35 then
				return 
			end
		end
	end
	
	local old_txt = recv_msg.msg
	recv_msg.msg = GmCommon.filter(recv_msg.msg)

	local send_msg = Msg.gc.GC_CHAT
	send_msg.msg_type = recv_msg.msg_type
	
	send_msg.role_name = human.name
	send_msg.obj_id = human.id
	send_msg.uuid = human._id
	
	send_msg.msg = recv_msg.msg
	send_msg.msg = ChatMsgSolve.hrefReplace(human, send_msg.msg) --CHAT事件
	
	if human.db.gm == 100 then
		send_msg.msg = "<font color='#ffc000'>" .. send_msg.msg .. "</font>"
	end

	local bluelv = 0
  if human.db.blueVip ~=0 then
    bluelv = human.db.blueVipLv
  end 
  send_msg.blue = string.format("%d%d%d", bluelv, human.db.blueYearVip, human.db.blueSuperVip) 
  send_msg.level3366 = human.db.level3366
  send_msg.args = 0
	if human.db.ban_say_time ~= 0 and Timer.now < human.db.ban_say_time then
		Msg.send(send_msg, human.fd)
		return
	end
	 
  if GmCommon.filter(old_txt, Dict2) ~= old_txt then
    local msg = Msg.lw.LW_GM
    msg.cmd = "chatreport"
    msg.arg =  GmCommon.filter(old_txt, Dict3)
    Msg.send2world(msg, human._id)
  end
	
	if send_msg.msg_type == CHAT_TYPE_WORLD then --世界聊天
		Msg.sendAll(send_msg)

		local msgMiddle = Msg.lw.LW_MIDDLE_CHAT
		msgMiddle.msg_type = send_msg.msg_type
		if Config.IS_MIDDLE == true then
			msgMiddle.role_name = send_msg.role_name
		else
			msgMiddle.role_name = "[" .. human.db.svrIndex%100000 .. "]" .. send_msg.role_name
		end
		msgMiddle.obj_id = send_msg.obj_id
		msgMiddle.uuid = send_msg.uuid
		msgMiddle.msg = send_msg.msg
		Msg.send2world(msgMiddle, human._id)	
	elseif send_msg.msg_type == CHAT_TYPE_LABA then --喇叭
    if BagLogic.getItemCount(human, ItemDefine.ITEM_ID_LABA) < 1 then
      if ObjHuman.getYuanbao(human) < 5 then
        return ShopHelp.onNoRMB(human)
      else
        ObjHuman.decYuanbao(human, 5, "chat")
      end
    else
      BagLogic.delItemsById(human, ItemDefine.ITEM_ID_LABA, 1, "chat")
    end
    
		Msg.sendAll(send_msg)
	elseif send_msg.msg_type == CHAT_TYPE_NEAR then --附近聊天
		Obj.sendArea(send_msg, human.scene_id, human.x, human.y)
	elseif send_msg.msg_type == CHAT_TYPE_GUILD then --军团
    local nMsg = {type = 3, msg = send_msg}
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_FAMILY, nMsg)
    Log.write(Log.LOGID_OSS_FAMILY_CHAT, human._id, human.account, human.name, human.ip, human.familyName, recv_msg.msg)
	elseif send_msg.msg_type == CHAT_TYPE_TEAM then --组队
    local team_msg = Msg.lw.LW_TEAM_CHAT
		team_msg.msg_type = send_msg.msg_type  
		team_msg.role_name = send_msg.role_name
		team_msg.obj_id = send_msg.obj_id
		team_msg.uuid = send_msg.uuid    
		team_msg.msg = send_msg.msg
		Msg.send2world(team_msg, human._id)
	elseif send_msg.msg_type == CHAT_TYPE_ACTIVITY then--活动		

	end
	
end


function send_err(human, err, a)
	local mm = Msg.gc.GC_CHAT_JUBAO_ERR
	if a then
		mm.err = string.format(err, a)
	else
		mm.err = err
	end
	Msg.send(mm, human.fd)	
end

local colorTable = {
	"#FFFFFF",			    --白色
	"#00EC1C",				--绿色
	"#1FABE1",				--蓝色
	"#DD61E2",				--紫色
	"#EB7B29",				--橙色
	"#FF1515",				--红色
	"#F1FF0A",				--金色
}

function showSth(human, content, color, cmd, arg1, arg2)
	local mm = Msg.gc.GC_CHAT
	mm.msg_type = CHAT_TYPE_WORLD
	mm.role_name = human.name
	mm.obj_id = human.id
	mm.uuid = human._id
    mm.level3366 = human.db.level3366
	local bluelv = 0
  if human.db.blueVip ~=0 then
    bluelv = human.db.blueVipLv
  end 
  mm.blue = string.format("%d%d%d", bluelv, human.db.blueYearVip, human.db.blueSuperVip) 
  
	local args = ""
	if arg1 then
		args = args .. "|" .. arg1
	end
	if arg2 then
		args = args .. "|" .. arg2
	end	
	mm.msg = "<u><a href=\'event:" .. cmd .. args .. "\'><font color='" .. colorTable[color] .. "'>【" .. content .. "】</font></a></u>"
	Msg.sendWorld(mm)
end


function LL_TEST(fd, msg, human, srclogic)
  print("msg.uuid:",msg.uuid, " srcLogic:",srclogic, " local:", _G.logic_id.. " aaaa:"..msg.aaa)
  if _G.logic_id ~= 0 then      
      Msg.logic2logic(msg, fd, srclogic)
  end
end


function WL_ROBOT_TEST(fd, msg)
   local mm = Msg.lw.LW_ROBOT_TEST
   mm.type = msg.type
   mm.logicid = _G.logic_id
   if msg.type == 1 then
       mm.result = ""..math.floor(_G.collectgarbage("count")).."KB"
       Msg.send2world(mm, "")
   end
end
